package org.example.everyday;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/2/23 9:26
 */
public class Lc917 {

    public String reverseOnlyLetters(String s) {
        char[] charArray = s.toCharArray();
        int left  = 0, right = charArray.length - 1;
        while (true){
            while(left < right && !Character.isLetter(s.charAt(left))){
                left++;
            }
            while(right > left && !Character.isLetter(s.charAt(right))){
                right--;
            }
            if(left >= right){
                break;
            }
            swap(charArray,left,right);
            left++;
            right--;
        }
        return new String(charArray);
    }

    private void swap(char[] arr,int left,int right){
        char temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
    }

    public static void main(String[] args) {
        Lc917 lc917 = new Lc917();
        String s = "a-bC-dEf-ghIj";
        String result = lc917.reverseOnlyLetters(s);
        System.out.println(result);
    }
}
